package com.example.question.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.question.entity.QuestionSet;
import com.example.question.entity.Positions;
import com.example.question.mapper.QuestionSetMapper;
import com.example.question.service.QuestionSetService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 题目集服务实现类
 * 实现QuestionSetService接口定义的所有业务方法
 * 继承ServiceImpl获取MyBatis-Plus提供的基础CRUD功能
 * @author 颜殿
 */
@Service
public class QuestionSetServiceImpl extends ServiceImpl<QuestionSetMapper, QuestionSet> implements QuestionSetService {

    /**
     * 添加题目集
     * 调用MyBatis-Plus的save方法保存实体
     * @param questionSet 题目集实体
     * @return 添加是否成功
     */
    @Override
    public boolean addQuestionSet(QuestionSet questionSet) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        questionSet.setCreatedAt(now);
        questionSet.setUpdatedAt(now);
        return save(questionSet);
    }

    /**
     * 删除题目集
     * 调用MyBatis-Plus的removeById方法删除记录
     * @param id 要删除的题目集ID
     * @return 删除是否成功
     */
    @Override
    public boolean deleteQuestionSet(Integer id) {
        return removeById(id);
    }

    /**
     * 修改题目集
     * 调用MyBatis-Plus的updateById方法更新记录
     * @param questionSet 包含更新信息的题目集实体
     * @return 更新是否成功
     */
    @Override
    public boolean updateQuestionSet(QuestionSet questionSet) {
        // 设置更新时间
        questionSet.setUpdatedAt(LocalDateTime.now());
        return updateById(questionSet);
    }

    /**
     * 根据职位ID分页查询题目集
     * 使用MPJLambdaWrapper构建联表查询条件
     * 如果positionId不为空，则添加职位条件
     * @param pageNum 页码，从1开始
     * @param pageSize 每页记录数
     * @param positionId 职位ID，可为null表示查询所有职位
     * @return 分页结果，包含题目集列表和分页信息
     */
    @Override
    public Page<QuestionSet> pageByPosition(Integer pageNum, Integer pageSize, Integer positionId) {
        MPJLambdaWrapper<QuestionSet> wrapper = new MPJLambdaWrapper<QuestionSet>()
            .selectAll(QuestionSet.class)
            .select("p.id as 'position.id'")
            .select("p.position_name as 'position.positionName'")
            .select("p.position_level as 'position.positionLevel'")
            .select("p.status as 'position.status'")
            .leftJoin(Positions.class, "p", Positions::getId, QuestionSet::getPositionsId);

        if (positionId != null) {
            wrapper.eq(QuestionSet::getPositionsId, positionId);
        }

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    /**
     * 根据职位级别分页查询题目集
     * 使用MPJLambdaWrapper构建查询条件
     * @param pageNum 页码，从1开始
     * @param pageSize 每页记录数
     * @param positionLevel 职位级别，可为null表示查询所有级别
     * @return 分页结果，包含题目集列表和分页信息
     */
    @Override
    public Page<QuestionSet> pageByPositionLevel(Integer pageNum, Integer pageSize, String positionLevel) {
        MPJLambdaWrapper<QuestionSet> wrapper = new MPJLambdaWrapper<QuestionSet>()
            .selectAll(QuestionSet.class)
            .select("p.id as 'position.id'")
            .select("p.position_name as 'position.positionName'")
            .select("p.position_level as 'position.positionLevel'")
            .select("p.status as 'position.status'")
            .leftJoin(Positions.class, "p", Positions::getId, QuestionSet::getPositionsId);

        if (positionLevel != null && !positionLevel.isEmpty()) {
            wrapper.eq("p.position_level", positionLevel);
        }

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    /**
     * 根据状态查询题目集
     * 使用MPJLambdaWrapper构建查询条件
     * 如果status不为空，则添加状态条件
     * @param pageNum 页码，从1开始
     * @param pageSize 每页记录数
     * @param status 状态值，可为null表示查询所有状态
     * @return 分页结果，包含题目集列表和分页信息
     */
    @Override
    public Page<QuestionSet> pageByStatus(Integer pageNum, Integer pageSize, Integer status) {
        MPJLambdaWrapper<QuestionSet> wrapper = new MPJLambdaWrapper<QuestionSet>()
            .selectAll(QuestionSet.class)
            .select("p.id as 'position.id'")
            .select("p.position_name as 'position.positionName'")
            .select("p.position_level as 'position.positionLevel'")
            .select("p.status as 'position.status'")
            .leftJoin(Positions.class, "p", Positions::getId, QuestionSet::getPositionsId);

        if (status != null) {
            wrapper.eq(QuestionSet::getStatus, status);
        }

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public Page<QuestionSet> pageByCondition(Integer pageNum, Integer pageSize, Integer positionsId, String level, Integer status) {
        // 构建基础查询
        MPJLambdaWrapper<QuestionSet> wrapper = new MPJLambdaWrapper<QuestionSet>()
                .select(QuestionSet::getId)
                .select(QuestionSet::getName)
                .select(QuestionSet::getOrderNum)
                .select(QuestionSet::getPositionsId)
                .select(QuestionSet::getStatus)
                .select(QuestionSet::getCreatedAt)
                .select(QuestionSet::getUpdatedAt)
                .select("p.id as 'position.id'")
                .select("p.position_name as 'position.positionName'")
                .select("p.position_level as 'position.positionLevel'")
                .select("p.status as 'position.status'")
                .leftJoin(Positions.class, "p", Positions::getId, QuestionSet::getPositionsId);

        // 添加职位ID条件
        if (positionsId != null) {
            wrapper.eq(QuestionSet::getPositionsId, positionsId);
        }

        // 添加级别条件
        if (level != null && !level.isEmpty()) {
            wrapper.eq("p.position_level", level);
        }

        // 添加状态条件
        if (status != null) {
            wrapper.eq(QuestionSet::getStatus, status);
        }

        // 添加排序
        wrapper.orderByDesc(QuestionSet::getCreatedAt);

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public Positions getPositionByQuestionSetId(Long id) {
        return baseMapper.selectPositionByQuestionSetId(id);
    }
}